/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.framework.protocol.dubbo;

import com.kason.framework.Invocation;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.Callable;

/**
 * @author 卢清波
 * @date 2021/2/20 11:14
 */
public class NettyClientHandler extends SimpleChannelInboundHandler implements Callable
{

    private Invocation invocation ;
    private String result = "";
    private ChannelHandlerContext channelHandlerContext ;
    public void setInvocation(Invocation invocation)
    {
        this.invocation = invocation;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        channelHandlerContext = ctx;
    }

    @Override
    protected synchronized void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception
    {

        result = (String) o;
        System.out.println("接收到数据信息："+result);
        notify();
    }


    @Override
    public synchronized Object call() throws Exception
    {
       channelHandlerContext.writeAndFlush(this.invocation);
       wait();
       return result;
    }
}
